Skip to content

⚡ Bolt: Batch database inserts to resolve N+1 bottlenecks#35

Open
bobdivx wants to merge 1 commit into
devfrom
bolt-batch-inserts-6281506081119408344
Open

⚡ Bolt: Batch database inserts to resolve N+1 bottlenecks#35
bobdivx wants to merge 1 commit into
devfrom
bolt-batch-inserts-6281506081119408344

Conversation

@bobdivx
Copy link
Copy Markdown
Owner

@bobdivx bobdivx commented May 15, 2026

💡 What: Refactored multiple files where database inserts were happening iteratively inside a loop. I replaced them with a single map() operation to build an array, followed by a single, batched await db.insert().values([...]) call.
🎯 Why: To resolve N+1 query bottlenecks during database insertions (like missing model seeding or plan generation). Drizzle ORM supports batch inserts, which drastically reduces database round-trips.
📊 Impact: Reduces database calls for M items from M inserts down to 1 batch insert query, improving performance and avoiding unnecessary event-loop blocking.
🔬 Measurement: Verify with pnpm test and by observing faster initialization in src/pages/api/models.ts and request planning in src/pages/api/forge-chat.ts.


PR created automatically by Jules for task 6281506081119408344 started by @bobdivx

Refactored multiple files where database inserts were happening inside a loop, replacing them with mapping arrays and batch inserts to improve database efficiency.

Co-authored-by: bobdivx <6737167+bobdivx@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
forge Ready Ready Preview, Comment May 15, 2026 5:51pm

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes database performance by replacing iterative single-record inserts with batch inserts across several API endpoints, including forge-chat-stream, forge-chat, and models. These changes effectively eliminate N+1 query bottlenecks during bulk operations. Feedback for src/pages/api/models.ts suggests further reducing database round-trips by updating the local dbCatalog array directly instead of re-fetching it from the database after the batch insert.

Comment thread src/pages/api/models.ts
}));
if (toInsert.length > 0) {
await db.insert(AgentModel).values(toInsert);
dbCatalog = await db.select().from(AgentModel);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of re-fetching the entire catalog from the database after the batch insert, you can update the local dbCatalog array directly using the toInsert data. This avoids an unnecessary database round-trip, which aligns with the goal of this PR to improve performance by reducing redundant queries.

Suggested change
dbCatalog = await db.select().from(AgentModel);
dbCatalog.push(...toInsert);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant